/**
 * 
 */
package com.mva.common;

import java.text.*;
import com.mva.Address_DAO.AddressDAO;
import com.mva.Address_DAO.AddressDB;
import com.mva.Codes_DAO.CodeValueDAO;
import com.mva.Codes_DAO.CodeValueDB;
import com.mva.Vendor_DAO.VendorDAO;
import com.mva.Vendor_DAO.VendorDB;

/*----------------------------------------------------------------------------*/
/* Date:    2012-02-21                                                        */
/* Author:  David Erekson                                                     */
/* Purpose: MVA overall unit validation                                       */
/*----------------------------------------------------------------------------*/
public abstract class Validator
{
	// Declare class variables
	private static AddressDAO   addressDAO   = null;
	private static CodeValueDAO codevalueDAO = null;
	private static VendorDAO    vendorDAO    = null;

	public String numberFormatter (double d)
	{
		NumberFormat nf = NumberFormat.getCurrencyInstance();
		return nf.format(d);
	}

	public String validatePhone (String Phn)
	{
		String inPhn  = Phn.trim();
		String outPhn = "";
		if (inPhn.length() < 10)
		{
			System.out.println("Phone string less than 10 digits");
			return "**BAD**";
		}
		outPhn = Formatter.formatPhone(inPhn);
		if (outPhn.equals("**BAD**"))
		{
			System.out.println("Phone string less than 10 digits");
			return outPhn;
		}
		return outPhn;
	}

	public boolean validateEmail (String Eml)
	{
		String inEml  = Eml.trim();
		if (inEml.length() > 60)
		{
			System.out.println("Email string more than 60 characters");
			return false;
		}
		if (inEml.indexOf('@') == -1)
		{
			System.out.println("Email string does not contain '@'");
			return false;
		}
		if (inEml.indexOf(".net") == -1 &&
				inEml.indexOf(".com") == -1 &&
				inEml.indexOf(".gov") == -1 &&
				inEml.indexOf(".biz") == -1   )
		{
			System.out.println("Email string does not contain valid domain type");
			return false;
		}
		return true;
	}

	public static String validateZip (String Zp)
	{
		String inZp  = Zp.trim();
		String outZp = "";
		if (inZp.length() < 5)
		{
			System.out.println("Zip string less than 5 digits");
			return "**BAD**";
		}
		outZp = Formatter.formatZip(inZp);
		if (outZp.equals("**BAD**"))
		{
			System.out.println("Zip string invalid");
			return outZp;
		}
		return outZp;
	}

	public static boolean isVendorValid (String Vndr)
	{
		String    tmpVndr   = Vndr.trim();
		tmpVndr   = tmpVndr.toUpperCase();
		vendorDAO = DAOFactory.getVendorDAO();
		VendorDAO vDAO      = new VendorDB();
		boolean   isValid   = vDAO.existsVendor(tmpVndr);
		return isValid;
	}

	public static boolean isCodeValid (String Sys, String Sgmt, String Cd)
	{
		String       tmpSys       = Sys.trim();
		tmpSys       = tmpSys.toUpperCase();
		String       tmpSgmt      = Sgmt.trim();
		tmpSgmt      = tmpSgmt.toUpperCase();
		String       tmpCd        = Cd.trim();
		tmpCd        = tmpCd.toUpperCase();
		codevalueDAO = DAOFactory.getCodeValueDAO();
		CodeValueDAO cvDAO        = new CodeValueDB();
		boolean      isValid      = cvDAO.existsCode(tmpSys, tmpSgmt, tmpCd);
		return isValid;
	}

	public static boolean isAddressValid (int Addr)
	{
		addressDAO         = DAOFactory.getAddressDAO();
		AddressDAO aDAO    = new AddressDB();
		boolean    isValid = aDAO.existsAddress(Addr);
		return isValid;
	}

	/*----------------------------------------------------------------------------*/
	/* Date:    2012-02-21                                                        */
	/* Author:  David Erekson                                                     */
	/* Purpose: MVA overall unit validation                                       */
	/*----------------------------------------------------------------------------*/

	// new code goes here

}
